package com.ly.hotel.spainterface.jg.utils;

import android.os.Handler;
import android.os.Message;

import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.jg.bean.OnSwipeCardListener;
import com.ly.hotel.view.floatWindows.ShuaKaFloatWin;

/**
 * Created by ThinkPad on 2018/3/7.
 */

public class SwipeCardUtils {
    private OnSwipeCardListener onSwipeCardListener;
    private boolean mBandNoRequest = false;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    // 更新本页面文本框显示读取到的卡号
                    if(MyApplication.getBandNo().equals("")){
                        if(this != null){
                            return;
                        }
                    }else{
                        String cardId = MyApplication.getBandNo();
                        onSwipeCardListener.swipeCardListener(cardId);
                    }
                    break;

                default:
                    break;
            }
        }
    };

    public void swipecard(){
        ShuaKaFloatWin.showShuakaWin();
        new Thread(){
            @Override
            public void run() {
                mBandNoRequest = true;
                MyApplication.clearBandNo();
                while (MyApplication.getBandNo() == "" && mBandNoRequest == true ){
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                mBandNoRequest = false;
                ShuaKaFloatWin.hideShuakaWin();
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        }.start();
    }

    public void cancelBandNo(Boolean mBandNoRequest){// 传入false就会取消正在等待读取刷卡接口
        this.mBandNoRequest = mBandNoRequest;
    }

    public void setOnSwipeCardListener(OnSwipeCardListener onSwipeCardListener){
        this.onSwipeCardListener = onSwipeCardListener;
    }
}
